package com.vue.admin.service.common.mybatis.dialect;

/**
 * @program: tk-robot-service
 * @description: oracle数据库方言
 * @author: zhangyangyang
 * @create: 2020-01-07 10:26
 **/
public class OracleDialect extends Dialect {

	@Override
	public String getCountString(String sql) {
		// TODO Oracle分页查询
		return null;
	}

	@Override
	public String getLimitString(String sql, int offset, int limit) {
		String nsql = sql.trim();
		boolean isForUpdate = false;
		if(nsql.toLowerCase().endsWith(" for update")) {
			nsql = nsql.substring(0, nsql.length() - 11);
			isForUpdate = true;
		}
		StringBuffer pagingSelect = new StringBuffer(nsql.length() + 100);
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(nsql);
		pagingSelect.append(" ) row_ ) where rownum_ > " + offset + " and rownum_ <= " + (offset + limit));
		if(isForUpdate) {
			pagingSelect.append(" for update");
		}
		return pagingSelect.toString();
	}
}
